package com.ruoyi.framework.config;

import com.ruoyi.common.utils.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Bean(name ="redissonClient")
    public RedissonClient initRedisson(){
        // 1.构造redisson实现分布式锁必要的Config
        Config config = new Config();

        SingleServerConfig singleServerConfig = config.useSingleServer();
        String address = "redis://" + host + ":" + port;
        singleServerConfig.setAddress(address);
        if (StringUtils.isNotEmpty(password)){
            singleServerConfig.setPassword(password);
        }
        singleServerConfig.setDatabase(10);


        // 2.构造RedissonClient
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }
}
